package com.letoken.platform.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.AdminLeTokenService;
import com.letoken.platform.admin.util.DateTimeUtil;
import com.letoken.platform.admin.util.LineChartUtil;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.util.BigDecimalUtil;
import com.letoken.platform.repo.admin.manager.AdminIssueConfigManager;
import com.letoken.platform.repo.admin.po.AdminIssueConfig;
import com.letoken.platform.repo.admin.req.user.QueryAccelerateListReq;
import com.letoken.platform.repo.admin.req.user.QueryCirculationListReq;
import com.letoken.platform.repo.admin.req.user.QueryIssueListReq;
import com.letoken.platform.repo.admin.req.user.QueryLineChartReq;
import com.letoken.platform.repo.admin.res.user.*;
import com.letoken.platform.repo.app.dao.AppUserCirculateMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueHistoryMapper;
import com.letoken.platform.repo.app.dao.AppUserIssueMapper;
import com.letoken.platform.repo.utlis.PageDataUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;

/**
 * @author letokenlabs
 * @date 2022/11/23$
 */
@Service
@Log4j2
public class AdminLeTokenServiceImpl implements AdminLeTokenService {

    @Autowired
    private AppUserIssueMapper appUserIssueMapper;
    @Autowired
    private AdminIssueConfigManager issueConfigManager;

    @Autowired
    private AppUserCirculateMapper circulateMapper;

    @Autowired
    private AppUserIssueHistoryMapper issueHistoryMapper;

    /**
     * 铸币发行记录
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryIssueListRes> queryIssueList(QueryIssueListReq req) {
        AdminIssueConfig adminIssueConfig = issueConfigManager.queryLet();
        Page<QueryIssueListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryIssueListRes> issuePage = appUserIssueMapper.queryIssueList(page, req);
        List<QueryIssueListRes> records = issuePage.getRecords();
        for (QueryIssueListRes record : records) {
            Integer rewardRate = adminIssueConfig.getRewardRate();
            BigDecimal multiply = BigDecimalUtil.multiply(new BigDecimal(record.getIssueAmount()), new BigDecimal(rewardRate).divide(new BigDecimal(100)));
            record.setWaitCirculation(multiply.toString());
        }
        return PageDataUtil.getPageData(issuePage);
    }

    /**
     * 铸币流通记录
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryCirculationListRes> queryCirculationList(QueryCirculationListReq req) {
        Page<QueryCirculationListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryCirculationListRes> circulation = appUserIssueMapper.queryCirculationList(page, req);
        return PageDataUtil.getPageData(circulation);
    }

    /**
     * 铸币加速流通记录
     *
     * @param req
     * @return
     */
    @Override
    public PageData<QueryAccelerateListRes> queryAccelerateList(QueryAccelerateListReq req) {
        Page<QueryAccelerateListRes> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<QueryAccelerateListRes> accelerate = issueHistoryMapper.queryAccelerateList(page, req);
        return PageDataUtil.getPageData(accelerate);
    }

    @Override
    public QueryIssueCountRes queryIssueCount() {
        QueryIssueCountRes res = appUserIssueMapper.queryIssueCount();
        res.setTodayIssueCount(BigDecimalUtil.twoScaleDown(res.getTodayIssueCount()));
        res.setYesterdayIssueCount(BigDecimalUtil.twoScaleDown(res.getYesterdayIssueCount()));
        res.setThisMonthIssueCount(BigDecimalUtil.twoScaleDown(res.getThisMonthIssueCount()));
        res.setIssueCount(BigDecimalUtil.twoScaleDown(res.getIssueCount()));
        res.setWaitCirculationTotal(BigDecimalUtil.twoScaleDown(res.getWaitCirculationTotal()));
        return res;
    }

    @Override
    public QueryCirculationCountRes queryCirculationCount() {
        QueryCirculationCountRes res = circulateMapper.queryCirculationCount();
        res.setTodayCirculationCount(BigDecimalUtil.twoScaleDown(res.getTodayCirculationCount()));
        res.setYesterdayCirculationCount(BigDecimalUtil.twoScaleDown(res.getYesterdayCirculationCount()));
        res.setThisMonthCirculationCount(BigDecimalUtil.twoScaleDown(res.getThisMonthCirculationCount()));
        res.setCirculationCount(BigDecimalUtil.twoScaleDown(res.getCirculationCount()));
        res.setWaitCirculationCount(BigDecimalUtil.twoScaleDown(res.getWaitCirculationCount()));
        return res;
    }

    @Override
    public List<QueryLineChartRes> queryIssueLineChart(QueryLineChartReq req) {
        List<LocalDate> lineChartTime = DateTimeUtil.getLineChartTime(req);
        List<QueryLineChartRes> lineChartRes = appUserIssueMapper.queryIssueLineChart(req);
        return LineChartUtil.getLineChartRes(lineChartTime, lineChartRes);

    }

    @Override
    public List<QueryLineChartRes> queryCirculationLineChart(QueryLineChartReq req) {
        List<LocalDate> lineChartTime = DateTimeUtil.getLineChartTime(req);
        List<QueryLineChartRes> lineChartRes = circulateMapper.queryCirculationLineChart(req);
        return LineChartUtil.getLineChartRes(lineChartTime, lineChartRes);
    }

    @Override
    public QueryAccelerateCountRes queryAccelerateCount() {
        QueryAccelerateCountRes res = issueHistoryMapper.queryAccelerateCount();
        if (res != null){
            if (res.getTodayAccelerateCount() != null){
                res.setTodayAccelerateCount(BigDecimalUtil.twoScaleDown(res.getTodayAccelerateCount()));
            }
            if (res.getYesterdayAccelerateCount() != null){
                res.setYesterdayAccelerateCount(BigDecimalUtil.twoScaleDown(res.getYesterdayAccelerateCount()));
            }
            if (res.getThisMonthAccelerateCount() != null){
                res.setThisMonthAccelerateCount(BigDecimalUtil.twoScaleDown(res.getThisMonthAccelerateCount()));
            }
            if (res.getAccelerateCount() != null){
                res.setAccelerateCount(BigDecimalUtil.twoScaleDown(res.getAccelerateCount()));
            }
        }else {
            res = new QueryAccelerateCountRes();
        }
        return res;
    }
}
